<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 会员管理
// +----------------------------------------------------------------------
namespace app\admin\controller\member2;

use app\admin\model\member\Member as MemberModel;
use app\common\controller\Adminbase;
use app\member\service\User;
use think\Db;

class Salesperson extends Adminbase
{
    protected $searchFields = 'id,nickname';

    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $where[] = ['sa.type', 'in', [1,2]];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $where[] = ['sa.id', '=', $filter['id']];
                }
                if (isset($filter['name'])) {
                    $where[] = ['sa.name', 'like', '%' . $filter['name'] . '%'];
                }
                if (isset($filter['phone'])) {
                    $where[] = ['sa.phone', 'like', '%' . $filter['phone'] . '%'];
                }
                if (isset($filter['type'])) {
                    $where[] = ['sa.type', '=', $filter['type']];
                }
                if (isset($filter['status'])) {
                    $where[] = ['sa.status', '=', $filter['status']];
                }

                if (isset($filter['no'])) {
                    $where[] = ['sa.no', 'like', '%' . $filter['no'] . '%'];
                }
                if (isset($filter['username'])) {
                    $where[] = ['m.username', 'like', '%' . $filter['username'] . '%'];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['sa.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('salesperson')->alias('sa')
                ->leftJoin('member m','sa.member_id=m.id')
                ->field('sa.*,m.username')
                ->where($where)
                ->order('sa.create_time desc')
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $top_salesperson = Db::name('salesperson')->where('id', '=', $v['top_sid'])->find();
                if ($top_salesperson) {
                    $_list[$k]['top_info'] = $top_salesperson['id'] . '----' . $top_salesperson['name'];
                }
            }
            $total = Db::name('salesperson')->alias('sa')
                ->leftJoin('member m','sa.member_id=m.id')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    //增加账号
    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $username=Db::name('member')
                ->where('username','=',$data['username'])
                ->find();
            if ($username){
                $this->error('账号已存在！');
            }
            $timestamp = time();
            $lastFour = substr($timestamp, -6) . substr($timestamp, -2);
            $salt = generateRandomString(8) . generateRandomString(8);

            $member_insert = array(
                'username' => $data['username'],
                'create_time' => time(),
                'password' => encrypt_password(trim($data['password']), $salt),
                'tx_phone' => $data['phone'],
                'role' => 1,
//                'salesperson_id' => $salesperson_id,
                'reg_ip' => $this->request->ip(),
                'salt' => $salt,
            );
            $member_id = Db::name('member')->insertGetId($member_insert);

            $salesperson_insert = array(
                'name' => $data['name'],
                'sf_id' => $data['sf_id'],
                'phone' => $data['phone'],
                'avatar_img' => $data['avatar_img'],
                'sf_img1' => $data['sf_img1'],
                'sf_img2' => $data['sf_img2'],
                'type' => $data['type'],
                'create_time' => time(),
                'tg_no' => $lastFour,
                'member_id' => $member_id,
                'top_sid' => $data['top_sid'],
                'no' => $data['no'],
            );
            $salesperson_id = Db::name('salesperson')->insertGetId($salesperson_insert);

            if ($member_id) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
            return $data;
        }

        $sale_list= Db::name('salesperson')->where([
            'status' => 1,
        ])->select();
        $this->assign([
            'sale_list' => $sale_list
        ]);
        return $this->fetch();
    }

    public function edit()
    {
        $member_id = $this->request->param('id/d', 0);
        $data = Db::name('salesperson')->find($member_id);
        $update = array(
            'id' => $member_id,
            'status' => !$data['status'],
        );
        $result = Db::name('salesperson')->update($update);
        if ($result) {
            return ['code' => 200, 'data' => '', 'msg' => '切换成功'];
        } else {
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }

    public function edit2()
    {
        $id = $this->request->param('id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $username=Db::name('member')
                ->where([
                    ['username','=',$data['username']],
                    ['id','<>',$data['member_id']]
                ])
                ->find();
            if ($username){
                $this->error('账号已存在！');
            }
            $salesperson_update = array(
                'id' => $data['id'],
                'name' => $data['name'],
                'sf_id' => $data['sf_id'],
                'phone' => $data['phone'],
                //avatar_img
                'avatar_img' => $data['avatar_img'],
                'sf_img1' => $data['sf_img1'],
                'sf_img2' => $data['sf_img2'],
                'type' => $data['type'],
                'top_sid' => $data['top_sid'],
                'no' => $data['no'],
            );
            $stmt = Db::name('salesperson')->update($salesperson_update);
            if ($data['password']) {
                $salt = generateRandomString(8) . generateRandomString(8);
                $member_update = array(
//                    'username' => $data['username'],
                    'password' => encrypt_password(trim($data['password']), $salt),
                    'salt' => $salt
                );
                Db::name('member')->where([
                    ['id', '=', $data['member_id']]
                ])->update($member_update);
            }
            if ($data['username']) {
                $member_update = array(
                    'username' => $data['username'],
                );
                Db::name('member')->where([
                    ['id', '=', $data['member_id']]
                ])->update($member_update);
            }

            $this->success('操作成功！');
        }

        $data = Db::name('salesperson')->find($id);
        $member_info = Db::name('member')->find($data['member_id']);
        $sale_list= Db::name('salesperson')->where([
            'status' => 1,
        ])->select();
        $this->assign([
            'data' => $data,
            'member_info' => $member_info,
            'sale_list' => $sale_list
        ]);
        return $this->fetch();
    }

    public function funtlist()
    {
        $id = $this->request->param('sale_id/d', 0);

        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $where[] = ['type', 'in', [1, 3, 5]];
            $where[] = ['salesperson_id', '=', $id];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
            }

            $_list = Db::name('member_finance')
                ->where($where)
                ->page($page, $limit)
                ->order('create_time desc')
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($v['finish_time']) {
                    $_list[$k]['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                }
            }
            //获取数量
            $total = Db::name('member_finance')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign([
            'sale_id' => $id,
        ]);
        return $this->fetch();
    }

    public function tixian()
    {
        $sale_id = $this->request->param('sale_id/d', 0);
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $salesperson_info=Db::name('salesperson')->find($data['sale_id']);
            if ($data['money']<0){
                $this->error('请输入正确值');
            }
            if ($data['money']>$salesperson_info['money']){
                $this->error('提现金额大于余额');
            }
            setMoney($salesperson_info['member_id'],$salesperson_info['id'],0,$data['money'],2,0,0,3,3);
            $this->success('操作成功！');
        }
        $this->assign([
            'sale_id' => $sale_id
        ]);
        return $this->fetch();
    }

}
